#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

MODULE_LICENSE("GPL");

#define MAJOR_NUM 222

static int hzf_var = 0;

static ssize_t hzf_read(struct file *file, char __user *buf, size_t count,
		loff_t *offset)
{
	if(copy_to_user(buf, &hzf_var, sizeof(hzf_var)))
		return -EFAULT;

	return sizeof(hzf_var);
}

static ssize_t hzf_write(struct file *file, const char __user *buf,
		size_t count, loff_t *offset)
{
	if(copy_from_user(&hzf_var, buf, sizeof(int)))
		return -EFAULT;

	return sizeof(int);
}

static struct file_operations hzf_fops = 
{
    .owner = THIS_MODULE,
    .read = hzf_read,
    .write = hzf_write
};

static int __init hzf_init(void)
{
    int ret = 0;

    ret = register_chrdev(MAJOR_NUM, "hzfchar", &hzf_fops);

    if(ret)
        printk("register hzfchar fail");
    else
        printk("register success");

    return 0;
}

static void __exit hzf_cleanup (void)
{

    int ret = 0;

    unregister_chrdev(MAJOR_NUM, "hzfchar");

    if(ret)
        printk("unregister hzfchar fail");
    else
        printk("unregister success");
}

module_init(hzf_init);
module_exit(hzf_cleanup);
